<?php

namespace Api\Controller;
use Common\Controller\AdminBaseController;

class AdminOauthController extends AdminBaseController
{
    const OAUTH_KEY = 'oauth_settings';

    protected $setting_model;

    public function _initialize() {
        parent::_initialize();
        $this->setting_model = D("Common/Setting");
    }

    public function setting() {

        if (IS_POST) {
            $post = array_map('trim', I('post.'));
            $data = array(
                'key'   => self::OAUTH_KEY,
                'value' => json_encode($post)
            );
            if ($this->setting_model->save($data) !== false) {
                //定义回调URL通用的URL
                $url_callback = 'http://'.$_SERVER['HTTP_HOST'].'/index.php?g=api&m=oauth&a=callback&type=';
                $configs = array(
                    //腾讯QQ登录配置
                    'THINK_SDK_QQ'      => array(
                        'APP_KEY'       => $post['QQ_APP_ID'], //应用注册成功后分配的 APP ID
                        'APP_SECRET'    => $post['QQ_APP_KEY'], //应用注册成功后分配的KEY
                        'CALLBACK'      => $url_callback . 'qq',
                    ),
                    //新浪微博配置
                    'THINK_SDK_SINA'    => array(
                        'APP_KEY'       => $post['SINA_API_KEY'], //应用注册成功后分配的 APP ID
                        'APP_SECRET'    => $post['SINA_SECRET'],//应用注册成功后分配的KEY
                        'CALLBACK'      => $url_callback . 'sina',
                    ),
                    /*
                    //腾讯微博配置
                    'THINK_SDK_TENCENT' => array(
                        'APP_KEY'       => '', //应用注册成功后分配的 APP ID
                        'APP_SECRET'    => '', //应用注册成功后分配的KEY
                        'CALLBACK'      => $url_callback . 'tencent',
                    ),
                    //网易微博配置
                    'THINK_SDK_T163'    => array(
                        'APP_KEY'       => '', //应用注册成功后分配的 APP ID
                        'APP_SECRET'    => '', //应用注册成功后分配的KEY
                        'CALLBACK'      => $url_callback . 't163',
                    ),
                    //人人网配置
                    'THINK_SDK_RENREN'  => array(
                        'APP_KEY'       => '', //应用注册成功后分配的 APP ID
                        'APP_SECRET'    => '', //应用注册成功后分配的KEY
                        'CALLBACK'      => $url_callback . 'renren',
                    ),
                    //360配置
                    'THINK_SDK_X360'    => array(
                        'APP_KEY'       => '', //应用注册成功后分配的 APP ID
                        'APP_SECRET'    => '', //应用注册成功后分配的KEY
                        'CALLBACK'      => $url_callback . 'x360',
                    ),
                    //豆瓣配置
                    'THINK_SDK_DOUBAN'  => array(
                        'APP_KEY'       => '', //应用注册成功后分配的 APP ID
                        'APP_SECRET'    => '', //应用注册成功后分配的KEY
                        'CALLBACK'      => $url_callback . 'douban',
                    ),
                    //Github配置
                    'THINK_SDK_GITHUB'  => array(
                        'APP_KEY'       => '', //应用注册成功后分配的 APP ID
                        'APP_SECRET'    => '', //应用注册成功后分配的KEY
                        'CALLBACK'      => $url_callback . 'github',
                    ),
                    //Google配置
                    'THINK_SDK_GOOGLE'  => array(
                        'APP_KEY'       => '', //应用注册成功后分配的 APP ID
                        'APP_SECRET'    => '', //应用注册成功后分配的KEY
                        'CALLBACK'      => $url_callback . 'google',
                    ),
                    //MSN配置
                    'THINK_SDK_MSN'     => array(
                        'APP_KEY'       => '', //应用注册成功后分配的 APP ID
                        'APP_SECRET'    => '', //应用注册成功后分配的KEY
                        'CALLBACK'      => $url_callback . 'msn',
                    ),
                    //点点配置
                    'THINK_SDK_DIANDIAN'=> array(
                        'APP_KEY'       => '', //应用注册成功后分配的 APP ID
                        'APP_SECRET'    => '', //应用注册成功后分配的KEY
                        'CALLBACK'      => $url_callback . 'diandian',
                    ),
                    //淘宝网配置
                    'THINK_SDK_TAOBAO'  => array(
                        'APP_KEY'       => '', //应用注册成功后分配的 APP ID
                        'APP_SECRET'    => '', //应用注册成功后分配的KEY
                        'CALLBACK'      => $url_callback . 'taobao',
                    ),
                    //百度配置
                    'THINK_SDK_BAIDU'   => array(
                        'APP_KEY'       => '', //应用注册成功后分配的 APP ID
                        'APP_SECRET'    => '', //应用注册成功后分配的KEY
                        'CALLBACK'      => $url_callback . 'baidu',
                    ),
                    //开心网配置
                    'THINK_SDK_KAIXIN'  => array(
                        'APP_KEY'       => '', //应用注册成功后分配的 APP ID
                        'APP_SECRET'    => '', //应用注册成功后分配的KEY
                        'CALLBACK'      => $url_callback . 'kaixin',
                    ),
                    //搜狐微博配置
                    'THINK_SDK_SOHU'    => array(
                        'APP_KEY'       => '', //应用注册成功后分配的 APP ID
                        'APP_SECRET'    => '', //应用注册成功后分配的KEY
                        'CALLBACK'      => $url_callback . 'sohu',
                    ),
                    */
                );
                $result = x_set_sys_config($configs);
                if ($result) {
                    $this->success("设置成功");
                } else {
                    $this->error("设置失败");
                }

            } else {
                $this->error('设置失败');
            }
        } else {
            $sites = $this->setting_model->getSettingByKey(self::OAUTH_KEY);
            $this->assign('detail', $sites);
            $this->display();
        }

    }
}